package com.lsa.letcode.the31.permutation;

import org.junit.Assert;
import org.junit.Test;

public class PermutationTest {
	@Test
	public void testPermutation() {
		Solution s = new Solution();
		testPermutation(s, new int[] { 1, 2, 4, 3 }, new int[] { 1, 3, 2, 4 });
		testPermutation(s, new int[] { 1, 3, 2, 4 }, new int[] { 1, 3, 4, 2 });
		testPermutation(s, new int[] { 1, 3, 4, 2 }, new int[] { 1, 4, 2, 3 });
		testPermutation(s, new int[] { 1, 4, 2, 3 }, new int[] { 1, 4, 3, 2 });
		testPermutation(s, new int[] { 1, 4, 3, 2 }, new int[] { 2, 1, 3, 4 });
		
		
		testPermutation(s, new int[] { 1, 2, 4, 3, 5 }, new int[] {  1, 2, 4, 5, 3 });
		testPermutation(s, new int[] {  1, 2, 4, 5, 3 }, new int[] {  1, 2, 5, 3, 4 });
		testPermutation(s, new int[] {  1, 2, 5, 3, 4 }, new int[] {  1, 2, 5, 4, 3 });
		testPermutation(s, new int[] {  1, 2, 5, 4, 3 }, new int[] {  1, 3, 2, 4, 5 });
		
		testPermutation(s, new int[] { 1, 1, 5 }, new int[] { 1, 5, 1 });
		testPermutation(s, new int[] { 1, 5, 1 }, new int[] { 5, 1, 1 });
		testPermutation(s, new int[] { 5, 1, 1 }, new int[] { 1, 1, 5 });
		
		testPermutation(s, new int[] { 1, 1, 5, 1 }, new int[] { 1, 5, 1, 1 });
		
		testPermutation(s, new int[] { 1, 2 }, new int[] { 2, 1});
		testPermutation(s, new int[] { 2, 1 }, new int[] { 1, 2});
		
		testPermutation(s, new int[] { 5, 4, 3, 2, 1 }, new int[] { 1, 2, 3, 4, 5 });
	}

	private void testPermutation(Solution s, int[] seq, int[] expected) {
		s.nextPermutation(seq);
		Assert.assertArrayEquals(expected, seq);
	}

	@Test
	public void testReverse() {
		Solution s = new Solution();
		{
			int[] seq = { 1, 2, 3, 4 };
			s.reverse(seq);
			Assert.assertArrayEquals(new int[] { 4, 3, 2, 1 }, seq);
		}

		{
			int[] seq = { 1, 2, 3, 4, 5 };
			s.reverse(seq);
			Assert.assertArrayEquals(new int[] { 5, 4, 3, 2, 1 }, seq);
		}

		{
			int[] seq = { 1, 2, 3, 4, 5 };
			s.reverse(seq, 1, 3);
			Assert.assertArrayEquals(new int[] { 1, 4, 3, 2, 5 }, seq);
		}
		
		{
			int[] seq = { 1, 2, 3, 4, 5 };
			s.reverse(seq, 0, 3);
			Assert.assertArrayEquals(new int[] { 4, 3, 2, 1, 5 }, seq);
		}
		
		{
			int[] seq = { 1, 2, 3, 4, 5 };
			s.reverse(seq, 1, 4);
			Assert.assertArrayEquals(new int[] { 1, 5, 4, 3, 2 }, seq);
		}
	}
}